// 封装输入框
<template>
  <input
    :type="mytext"
    class="myipt"
    @input="handleinput"
    :value="value"
    :class="{success:statu,error:!statu}"
    @change="handlechange"
  />
</template>

<script>
export default {
  data() {
    return {
      // 没输入时，输入框是正常的
      statu: true,
    };
  },
  props: {
    mytext: String,
    value: String,
    rules: RegExp, //代表正则规则
    msg: String, // 代表字符串
  },
  mounted() {
    // console.log(this.value);
  },
  methods: {
    handleinput(e) {
      let value = e.target.value;
      if (this.rules) {
        // 如果符合 正则规则
        if (this.rules.test(value)) {
          this.statu = true;
        } else {
          this.statu = false;
        }
      }
      // 把事件传给到父亲
      this.$emit("input", value);
    },
    handlechange(e) {
      let value = e.target.value;
      if (this.rules && !this.rules.test(value)) {
        this.$toast.fail(this.msg || "输入错误");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.myipt {
  width: 318/360 * 100vw;
  height: 60px;
  outline: none;
  border: none;
  border-bottom: 3px solid #ccc;
  font-size: 20px;
  line-height: 60px;
}
.success {
  border-bottom-color: green;
}
.error {
  border-bottom-color: red;
}
</style>